import utils from '../util/printUtils.js';
import printTool from '../util/printTool.js';
export default {
	data() {
		return {
			titlePrintName: '',
			orderPrintName: '',
			deviceList: [],
			waybillDateil:{},
		}
	},

	onLoad() {
		this.getTitleName();
		this.getOrderName();
		this.getWaybillData();
		var that = this;
		// #ifdef APP-PLUS
		that.initPrinter();
		// #endif

	},
	methods: {
		getWaybillData() {
			let order = '{"waybill_id":100,"member_id":10,"logistics_id":1,"company_id":8,"line_id":1517,' +
				'"deliver_point":249,"arrival_point":384,"start_point":8,"transit_point":0,"transit_destination"' +
				':"佛山南海","deliver_point_name":"临沂","arrival_point_name":"白云","company_name":"物流公司",' +
				'"order_type":60,"waybill_sn":"T12023xxxx00003","goodsn":"xxxx-00003-01","pay_status":20,' +
				'"pay_time":1694423776,"status":500,"platform_id":0,"platform_amount":"0.00","all_amount":' +
				'"680.00","all_order_amount":"680.00","order_amount":"680.00","pickup_fee":"0.00","delivery_fee' +
				'":"0.00","information_fee":"0.00","transit_fee":"0.00","insurance_fee":"0.00","advance_amount"' +
				':"0.00","carry_order_amount":"0.00","now_order_amount":"680.00","receipt_order_amount":"0.00",' +
				'"month_order_amount":"0.00","deduct_order_amount":"0.00","carry_order_amount_no":"0.00",' +
				'"now_order_amount_no":"680.00","receipt_order_amount_no":"0.00","month_order_amount_no":"0.00"' +
				',"deduct_order_amount_no":"0.00","carry_pay_status":0,"now_pay_status":20,"receipt_pay_status":0' +
				',"month_pay_status":0,"deduct_pay_status":0,"information_pay_status":0,"information_pay_time":' +
				'null,"order_amount_pay":10,"pickup_fee_pay":20,"delivery_fee_pay":20,"information_fee_pay":20,' +
				'"transit_fee_pay":20,"insurance_fee_pay":10,"information_fee_pay2":10,"advance_status":0,"' +
				'delivery_type":1,"is_receipt":1,"receipt_type":0,"receipt_num":0,"is_unusual":1,"is_control"' +
				':1,"is_urgency":1,"goods_total_num":2,"goods_total_weight":"1500.00","goods_total_volume":"3.00"' +
				',"createtime":1694423775,"updatetime":1694511648,"deletetime":null,"remark":"","upstatus":0,' +
				'"platform_company":"","platform_sn":"","platform_type":0,"journey_id":39,"departtime":' +
				'1694499707,"deliverytime":1694511648,"platformtime":null,"address_id":95,"memberno":0,"' +
				'send_username":"刘xx","send_mobile":"131xxxxx555","send_card":"","send_address":"山东省临沂市蒙阴街道"' +
				',"send_province_id":0,"send_city_id":0,"send_area_id":0,"send_accountname":"","send_account":""' +
				',"send_bankname":"","take_username":"周xx","take_mobile":"136xxxxx384","take_address":' +
				'"广东省佛山市南海区平夏东小学","take_card":"","take_province_id":0,"take_city_id":0,"take_area_id"' +
				':0,"start_point_name":"蒙阴","goods":[{"goods_id":97,"waybill_id":100,"goods_name":"配件",' +
				'"goods_nums":"2","goods_weight":"0.00","goods_volume":"0.00","goods_pack":"其他","total_weight"' +
				':"1500.00","total_volume":"3.00"}],"createtime_date":"2023-09-11 17:16:15",' +
				'"platformtime_date":"--"}'
			this.waybillDateil = JSON.parse(order);
		},
		getTitleName() {
			var titlePrint = uni.getStorageSync(this.preutils.titlePrint);
			if (titlePrint) {
				this.titlePrintName = titlePrint.name;
			}
		},
		getOrderName() {
			var orderPrint = uni.getStorageSync(this.preutils.orderPrint);
			if (orderPrint) {
				this.orderPrintName = orderPrint.name;
			}
			return '';
		},
		settingTitlePrint(e) {
			uni.setStorageSync(this.preutils.titlePrint, e);
			this.titlePrintName = e.name;
			console.log(e)
		},
		settingOrderPrint(e) {
			uni.setStorageSync(this.preutils.orderPrint, e);
			this.orderPrintName = e.name;

		},
		printClear(e) {
			let that = this;
			if (e == 1) {
				uni.showModal({
					title: '确定清除标签打印机信息',
					content: '是否继续操作？',
					success: res => {
						if (res.confirm) {
							console.log('====')
							uni.setStorageSync(that.preutils.titlePrint, '');
							that.titlePrintName = '';
						}
					}
				});
			} else {
				uni.showModal({
					title: '确定清除运单打印机信息',
					content: '是否继续操作？',
					success: res => {
						if (res.confirm) {
							uni.setStorageSync(that.preutils.orderPrint, '');
							that.orderPrintName = '';
						}
					}
				});

			}
		},

		printTitleTest() {
			let that = this;
			uni.showModal({
				title: '确认测试打印货物标签',
				content: '是否继续操作？',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						printTool.printTitle(that.waybillDateil);
					}
				}
			});
		},
		printOrderTest() {
			let that = this;
			uni.showModal({
				title: '确认测试打印货物托运单',
				content: '是否继续操作？',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						printTool.printOrder(that.waybillDateil);
					}
				}
			});
		},
		printOrderTemplate(){
			uni.navigateTo({
				url: "/pages/mine/print/template/order/templateOrder1"
			});
		},
		printTitleTemplate(){
			uni.navigateTo({
				url: "/pages/mine/print/template/title/templateTitle1"
			});
		},
		// ToTest() {
		// 	uni.navigateTo({
		// 		url: "/pages/mine/print/test/printTest"
		// 	});
		// },
		initPrinter: function() {
			var that = this;
			that.deviceList = [];
			var main = plus.android.runtimeMainActivity();
			var Context = plus.android.importClass("android.content.Context");
			var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
			plus.android.importClass(BManager);
			var BAdapter = BManager.getAdapter();
			plus.android.importClass(BAdapter);
			var lists = BAdapter.getBondedDevices();
			plus.android.importClass(lists);
			var iterator = lists.iterator();
			plus.android.importClass(iterator);
			while (iterator.hasNext()) {
				var d = iterator.next();
				plus.android.importClass(d);
				var temp = {
					name: d.getName(),
					address: d.getAddress(),
					status: d.getBondState(),
					uuids: d.getUuids(),
					op: d
				};
				that.deviceList.push(temp);
			}
		},

		mySleep: async function(time) {
			await this.mypromise(time);
		},
		mypromise: function(time) {
			return new Promise((resolve) => setTimeout(resolve, time));
		}
	}
}